using BasicData.API.Application.Command;
using BasicData.API.Application.Mediator;
using BasicData.ErrorCode;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace BasicData.API.Controllers
{
    /// <summary>
    /// 产品信息软删除控制器
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class ProductionSoftDeleteController : ControllerBase
    {
        private readonly IProductionInfoMediator _productionInfoMediator;
        private readonly ILogger<ProductionSoftDeleteController> _logger;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="productionInfoMediator">生产信息中介者</param>
        /// <param name="logger">日志</param>
        public ProductionSoftDeleteController(
            IProductionInfoMediator productionInfoMediator,
            ILogger<ProductionSoftDeleteController> logger)
        {
            _productionInfoMediator = productionInfoMediator;
            _logger = logger;
        }

        /// <summary>
        /// 软删除产品信息
        /// </summary>
        /// <param name="id">产品信息ID</param>
        /// <param name="operator">操作人</param>
        /// <returns>删除结果</returns>
        [HttpDelete("{id}")]
        public async Task<APIResult<bool>> SoftDeleteProduction(Guid id, [FromQuery] string @operator)
        {
            try
            {
                _logger.LogInformation("开始软删除产品信息，ID：{Id}，操作人：{Operator}", id, @operator);

                if (id == Guid.Empty)
                {
                    return new APIResult<bool>
                    {
                        Code = ResultCode.删除失败,
                        Message = "产品信息ID不能为空",
                        Data = false
                    };
                }

                if (string.IsNullOrWhiteSpace(@operator))
                {
                    return new APIResult<bool>
                    {
                        Code = ResultCode.删除失败,
                        Message = "操作人不能为空",
                        Data = false
                    };
                }

                // 创建单个软删除命令
                var command = new SoftDeleteProductionCommand
                {
                    ProductionId = id,
                    Operator = @operator
                };

                // 调用中介者执行删除
                var result = await _productionInfoMediator.SoftDeleteProductionAsync(command);

                _logger.LogInformation("软删除产品信息完成，ID：{Id}，结果：{Result}", id, result.Code);
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "软删除产品信息时发生错误，ID：{Id}", id);
                return new APIResult<bool>
                {
                    Code = ResultCode.删除失败,
                    Message = $"软删除产品信息失败: {ex.Message}",
                    Data = false
                };
            }
        }

        ///// <summary>
        ///// 批量软删除产品信息
        ///// </summary>
        ///// <param name="ids">产品信息ID列表</param>
        ///// <param name="operator">操作人</param>
        ///// <returns>删除结果</returns>
        //[HttpPost("batch")]
        //public async Task<APIResult<bool>> BatchSoftDeleteProduction([FromBody] List<Guid> ids, [FromQuery] string @operator)
        //{
        //    try
        //    {
        //        _logger.LogInformation("开始批量软删除产品信息，ID数量：{Count}，操作人：{Operator}", ids?.Count ?? 0, @operator);

        //        if (ids == null || ids.Count == 0)
        //        {
        //            return new APIResult<bool>
        //            {
        //                Code = ResultCode.删除失败,
        //                Message = "产品信息ID列表不能为空",
        //                Data = false
        //            };
        //        }

        //        if (string.IsNullOrWhiteSpace(@operator))
        //        {
        //            return new APIResult<bool>
        //            {
        //                Code = ResultCode.删除失败,
        //                Message = "操作人不能为空",
        //                Data = false
        //            };
        //        }

        //        // 创建批量删除命令
        //        var command = new BatchDeleteProductionInfoCommand
        //        {
        //            ProductionInfoIds = ids,
        //            ModifiedBy = @operator
        //        };

        //        // 调用中介者执行删除
        //        var result = await _productionInfoMediator.BatchDeleteProductionInfosAsync(command);

        //        _logger.LogInformation("批量软删除产品信息完成，ID数量：{Count}，结果：{Result}", ids.Count, result.Code);
        //        return result;
        //    }
        //    catch (Exception ex)
        //    {
        //        _logger.LogError(ex, "批量软删除产品信息时发生错误，ID数量：{Count}", ids?.Count ?? 0);
        //        return new APIResult<bool>
        //        {
        //            Code = ResultCode.删除失败,
        //            Message = $"批量软删除产品信息失败: {ex.Message}",
        //            Data = false
        //        };
        //    }
        //}
    }
} 